package com.hscm.boss.mvc.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hscm.boss.common.enums.PublicStatus;
import com.hscm.boss.common.enums.ResponseCode;
import com.hscm.boss.common.exception.BossException;
import com.hscm.boss.common.utils.NoUtil;
import com.hscm.boss.dao.entity.PayProduct;
import com.hscm.boss.dao.entity.PayWay;
import com.hscm.boss.dao.entity.UserInfo;
import com.hscm.boss.dao.mapper.PayProductMapper;
import com.hscm.boss.dao.mapper.PayWayMapper;
import com.hscm.boss.dao.result.DicEntity;
import com.hscm.boss.mvc.model.from.product.PayProductFrom;
import com.hscm.boss.mvc.service.PayProductService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class PayProductServiceImpl implements PayProductService {

    @Resource
    private PayProductMapper payProductMapper;

    @Resource
    private PayWayMapper payWayMapper;

    @Override
    public IPage<PayProduct> pageList(Integer pageNo, Integer pageSize) {
        QueryWrapper<PayProduct> wrapper = new QueryWrapper<PayProduct>();
        wrapper.orderByDesc("edit_time");
        return payProductMapper.selectPage(Page.of(pageNo, pageSize), wrapper);
    }

    @Override
    public void addProduct(PayProductFrom payProductFrom) {
        ///产品的编码和名字是唯一得
        PayProduct product = payProductMapper.selectOne(new QueryWrapper<PayProduct>()
                .eq("product_code", payProductFrom.getProductCode()));

        if (product != null) {
            throw new BossException(ResponseCode.PAYMENT_CODE_EXISTS);
        }


        product = payProductMapper.selectOne(new QueryWrapper<PayProduct>()
                //去掉空格
                .eq("product_name", payProductFrom.getProductName().trim()));

        if (product != null) {
            throw new BossException(ResponseCode.PAYMENT_NAME_EXISTS);
        }

        //添加新的支付产品
        PayProduct payProduct = new PayProduct();
        payProduct.setId(NoUtil.getUUID());
        payProduct.setProductCode(payProductFrom.getProductCode());
        payProduct.setProductName(payProductFrom.getProductName());
        payProduct.setStatus(PublicStatus.DISABLE.name());
        payProduct.setEditTime(new Date());
        //备注 创建新的支付产品
        payProduct.setRemark("创建新的支付产品");

        payProductMapper.insert(payProduct);


    }

    @Override
    public List<DicEntity> queryProductDicList() {
        List<DicEntity> dicEntityList = payProductMapper.selectProductDicList();
        return dicEntityList;
    }

    @Override
    public void editStatus(String productCode) {
        PayProduct payProduct = payProductMapper.selectOne(new QueryWrapper<PayProduct>()
                .eq("product_code", productCode));


        if (payProduct == null) {
            throw new BossException(ResponseCode.PAYMENT_PRODUCT_NOT_EXISTS);
        }

        String updateStatus = PublicStatus.ENABLE.name();
        //根据数据库中的值进行判断
        if (PublicStatus.ENABLE.name().equals(payProduct.getStatus())) {
            updateStatus = PublicStatus.DISABLE.name();
        }


        //单独更新这一个字段 提升效率
        PayProduct update = new PayProduct();
        update.setId(payProduct.getId());
        update.setStatus(updateStatus);

        payProductMapper.updateById(update);

    }


    //添加事务  对两张表进行操作
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeProduct(String productCode) {

        //删除pay_product
        int delete = payProductMapper.delete(new QueryWrapper<PayProduct>()
                .eq("product_code", productCode));



        //删除pay_way表
        if(delete > 0){
            payWayMapper.delete(new QueryWrapper<PayWay>()
                    .eq("pay_product_code",productCode));
        }


    }

    @Override
    public List<PayProduct> matchProductName(String productName) {
        QueryWrapper<PayProduct> wrapper = new QueryWrapper<>();
        wrapper.like("product_name", productName)
                .orderByAsc("product_name");
        return payProductMapper.selectList(wrapper);
    }


}
